
#ifndef __M_PRODUCT_TEST_H__
#define __M_PRODUCT_TEST_H__

#include "m_include.h"
#include "ble_nus.h"
#include "blecom.h"
#define M_PRODUCT_TEST_FLAG_STORE_RETRY 3 // add by mlw at 20161201 15:09
typedef void (*cmd_handler_m_t)(cmd_packet_t *packet);

typedef __packed struct
{
    uint8_t key;
    cmd_handler_m_t handler;
} cmd_entry_m_t;

// 厂测标志位
typedef struct m_product_test_t
{
    uint8_t testItem; // 测试项目
    uint8_t testCnt;  // 重复测试次数计数
                      //    bool bBleConnect; // 蓝牙是否已连接
    bool testDone;    // 厂测是否完成

    // add by mlw at 20161201 14:38
    bool bPcbaFlagStoreEn; // true:写PCBA标志位操作开始
                           //    bool bProFlagWriteBegin; // true:写成品标志位操作开始
    bool bProFlagStoreEn;  // true:写成品标志位操作完成
    // bool bProFlagWriteFinished; // true:写成品标志位操作完成
    uint8_t flagStoreRetry; // 写PCBA标志操作失败重试次数，预设最大3次，超过3次不成功，则该次写标志位操作失败
    // uint8_t proFlagWriteRetryCnt; // 写PCBA标志操作失败重试次数，预设最大3次，超过3次不成功，则该次写标志位操作失败

} M_PRODUCT_TEST;

extern uint32_t volatile ProductTestFlag;
extern bool LanYa_Check_flag;

#define CMD_T90_SYNC_TIME_TO_APP 0x01                     // SmartBand_Set_Time
#define CMD_T90_PRODUCT_TEST_WRITE_SN 0x90                // Smartband_ProDuctTest_Get_SN
#define CMD_T90_PRODUCT_TEST_READ_SN 0x91                 // Smartband_ProDuctTest_Get_SN
#define CMD_T90_PRODUCT_TEST_READ_SENDOR_PRO 0x92         // Smartband_ProDuctTest_STart_sensor
#define CMD_T90_PRODUCT_TEST_READ_FIRMWARE_VERSION 0x93   // Smartband_ProDuctTest_get_softversion
#define CMD_T90_PRODUCT_TEST_BUTTON_PRESS 0x94            // Smartband_ProDuctTest_STart_button
#define CMD_T90_PRODUCT_TEST_OLED_DISPLAY 0x95            // Smartband_ProDuctTest_STart_oled
#define CMD_T90_PRODUCT_TEST_MOTOR_VIBRATION 0x96         // Smartband_ProDuctTest_STart_motor
#define CMD_T90_PRODUCT_TEST_CHARGE_DETECT 0x97           // Smartband_ProDuctTest_STart_powerrecover
#define CMD_T90_PRODUCT_TEST_PCBA_PASS_WRITE_FLAG 0x98    // Smartband_ProDuctTest_Wflag_pcb
#define CMD_T90_PRODUCT_TEST_PCBA_PASS_READ_FLAG 0x99     // Smartband_ProDuctTest_Rflag_pcb
#define CMD_T90_PRODUCT_TEST_PCB_PRO_PASS_WRITE_FLAG 0x9A // Smartband_ProDuctTest_Wflag_FinalTest
#define CMD_T90_PRODUCT_TEST_PCB_PRO_PASS_READ_FLAG 0x9B  // Smartband_ProDuctTest_Rflag_FinalTest
#define CMD_T90_PRODUCT_TEST_ALL_PASS_REBOOT_SLEEP 0x9C   // Smartband_ProDuctTest_sleep
#define CMD_T90_PRODUCT_TEST_WRITE_HARDWARE_VERSION 0x9D  // Smartband_ProDuctTest_set_PCBversion
#define CMD_T90_PRODUCT_TEST_READ_HARDWARE_VERSION 0x9E   // Smartband_ProDuctTest_get_PCBversion
#define CMD_T90_PRODUCT_TEST_READ_SENDOR_PCBA 0x9F        // Smartband_ProDuctTest_STart_sensor_CP

void app_t90_set_time(cmd_packet_t *packet);                            // CMDSetTime(cmd_packet_t *packet);
void app_t90_product_test_write_sn(cmd_packet_t *packet);               // CMD_ProDuctTest_Set_SN(cmd_packet_t *packet);
void app_t90_product_read_sn(cmd_packet_t *packet);                     // CMD_ProDuctTest_Get_SN (cmd_packet_t *packet);
void app_t90_product_test_read_sensor_pro(cmd_packet_t *packet);        // ProDuctTest_STart_sensor(cmd_packet_t *packet);
void app_t90_product_test_read_sensor_pcba(cmd_packet_t *packet);       // CMD_ProDuctTest_STart_sensor_CP(cmd_packet_t *packet);
void app_t90_product_test_button_press(cmd_packet_t *packet);           // CMD_ProDuctTest_STart_button(cmd_packet_t *packet);
void app_t90_product_test_oled_display(cmd_packet_t *packet);           // CMD_ProDuctTest_STart_oled(cmd_packet_t *packet);
void app_t90_product_test_motor_vibration(cmd_packet_t *packet);        // CMD_ProDuctTest_STart_motor(cmd_packet_t *packet);
void app_t90_product_test_charge_detect(cmd_packet_t *packet);          // CMD_ProDuctTest_STart_powerrecover(cmd_packet_t *packet);
void app_t90_product_test_pcba_write_flag(cmd_packet_t *packet);        // CMD_ProDuctTest_Wflag_pcb(cmd_packet_t *packet);
void app_t90_product_test_pcba_read_flag(cmd_packet_t *packet);         // CMD_ProDuctTest_Rflag_pcb(cmd_packet_t *packet);
void app_t90_product_test_pcb_pro_write_flag(cmd_packet_t *packet);     // CMD_ProDuctTest_Wflag_FinalTest(cmd_packet_t *packet);
void app_t90_product_test_pcb_pro_read_flag(cmd_packet_t *packet);      // CMD_ProDuctTest_Rflag_FinalTest(cmd_packet_t *packet);
void app_t90_product_test_reboot_sleep(cmd_packet_t *packet);           // CMD_ProDuctTest_sleep(cmd_packet_t *packet);
// void app_t90_product_test_read_firmware_version(cmd_packet_t *packet);  // CMD_ProDuctTest_get_softversion(cmd_packet_t *packet);
void app_t90_product_test_write_hardware_version(cmd_packet_t *packet); // CMD_ProDuctTest_set_PCBversion(cmd_packet_t *packet);
void app_t90_product_test_read_hardware_version(cmd_packet_t *packet);  // CMD_ProDuctTest_get_PCBversion(cmd_packet_t *packet);

uint32_t ReadProductflag(void);
void m_product_test_param_init(void);
void m_product_test_timer_init(void);
void Producttime_Start(void);
void Producttime_Stop(void);
uint8_t WriteFINALtestFlag(void);
uint8_t WritePCBtestFlag(void);
void m_product_test_button(void);

void m_product_test_flag_store(uint32_t cmd);
void m_product_test_flag_store_handle_cb(bool bSucceed);

//------------------------------------------------------
typedef enum
{
    PRODUCT_TEST_STATE_TEST_NONE = 0,
    PRODUCT_TEST_STATE_TEST_PCBA,
    PRODUCT_TEST_STATE_TEST_ASSEM,
} product_test_state_t;
product_test_state_t product_test_state_get(void);
void product_test_state_write(product_test_state_t write_state);
#endif
